package com.zx.web.servelt;

import com.zx.bean.Goods;
import com.zx.bean.GoodsType;
import com.zx.bean.PageBean;
import com.zx.web.service.GoodsService;
import com.zx.web.service.impl.GoodsServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 章鑫
 * @version V1.0
 * @Project 2022-09-05
 * @Package com.zx.web.servelt
 * @Description:
 * @Date 2022/9/14 11:57
 */
@WebServlet("/goodsservlet")
public class GoodsServlet extends BaseServlet{
    GoodsService service = new GoodsServiceImpl();
    /**
     * 获取商品并分页展示
     * @param req
     * @param resp
     * @return
     */
    public String getGoodsListByTypeId(HttpServletRequest req, HttpServletResponse resp){
        String typeId = req.getParameter("typeId");
        //获取pageNum和pageSize
        String pageNum = req.getParameter("pageNum");
        if(pageNum==null){
            pageNum="1";
        }
        String pageSize = req.getParameter("pageSize");
        if(pageSize==null){
            pageSize = "6";
        }
        //查询类型id为？的数据集合,并实现分页findGoodByTypeIdPage
        PageBean<Goods> pageBean = service.findGoodByTypeIdPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize),Integer.parseInt(typeId));
        req.setAttribute("pageBean",pageBean);
        return "forward:/goodsList.jsp";
    }

    /**
     * 获取商品详情，并响应到goodsDetail.jsp
     * @return
     */
    public String getGoodsById(HttpServletRequest req,HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        Goods goods = service.findGoodsDetail(id);
        GoodsType goodsType = service.findGoodsType(goods.getTypeid());
        goods.setGoodsType(goodsType);
        req.setAttribute("goods",goods);
        return "forward:/goodsDetail.jsp";
    }


}
